package com.wholesale.utils;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wholesale.R;
import com.wholesale.beans.Comment;
import com.wholesale.datas.WebServiceClient;

public class GoodsCommentListAdapter extends ArrayAdapter<Comment> {

	List<Comment> commentList = new ArrayList<Comment>();
	private ListView listView;
	private AsyncImageLoader loader;
	private boolean isAsyncLoadData;
	private LinearLayout loadingLayout;
	private int lastItem;
	// 总页数
	private int pageNum;
	// 当前页数
	private int curPage = 1;
	// 分类ID
	private int goodsId;

	// 查询条件
	private String condition;

	public GoodsCommentListAdapter(Context context, ListView listView,
			int goodsId) {
		super(context, 0);
		this.listView = listView;
		loader = AsyncImageLoader.getInstance();
		this.goodsId = goodsId;
	}

	public GoodsCommentListAdapter(Context context, List<Comment> commentList,
			ListView listView, int goodsId) {
		super(context, 0, commentList);
		this.listView = listView;
		loader = AsyncImageLoader.getInstance();
		this.goodsId = goodsId;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		GoodsCommentCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.commentsitem, null);
			viewCache = new GoodsCommentCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (GoodsCommentCache) rowView.getTag();
		}
		Comment comment = getItem(position);
		viewCache.getFrom().setText(comment.getUserName());
		if (comment.getAddTime() != null) {
			viewCache.getDate().setText(comment.getAddTime().toString());
		}
		viewCache.getContent().setText(comment.getContent());
		if (comment.getReply()) {
			viewCache.getContent().setTextColor(Color.BLUE);
		} else {
			viewCache.getContent().setTextColor(Color.BLACK);
		}
		return rowView;
	}

	public void setAsyncLoadData(int pageNum) {
		this.pageNum = pageNum;
		getLoadView();
		loadingLayout.setVisibility(View.GONE);
		listView.addFooterView(loadingLayout, null, false);
		listView.setFooterDividersEnabled(false);
		this.listView.setOnScrollListener(scrollListener);
	}

	private void getLoadView() {
		if (loadingLayout == null) {
			LinearLayout searchLayout = new LinearLayout(getContext());
			searchLayout.setOrientation(LinearLayout.HORIZONTAL);
			ProgressBar progressBar = new ProgressBar(getContext());
			progressBar.setPadding(0, 0, 15, 0);
			searchLayout.addView(progressBar, new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT));
			searchLayout.setGravity(Gravity.CENTER);

			TextView loadView = new TextView(getContext());
			loadView.setText("Loading......");
			loadView.setGravity(Gravity.CENTER);
			loadView.setTextSize((float) 16.0);
			loadView.setTextColor(Color.BLACK);
			loadingLayout = new LinearLayout(getContext());
			loadingLayout
					.setBackgroundResource(R.drawable.categories_cell_bg_on);
			// loadingLayout.addView(searchLayout, new
			// LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
			// LinearLayout.LayoutParams.WRAP_CONTENT));
			loadingLayout.addView(loadView, new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));
			loadingLayout.setGravity(Gravity.CENTER);
		}
	}

	OnScrollListener scrollListener = new OnScrollListener() {

		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			lastItem = firstVisibleItem + visibleItemCount - 1;
		}

		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub
			if (lastItem == getCount()
					&& scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
				curPage = curPage + 1;
				if (curPage <= pageNum) {
					loadingLayout.setVisibility(View.VISIBLE);
					Thread d = new Thread(new Runnable() {
						public void run() {
							commentList = WebServiceClient.getComments(goodsId,
									curPage);
							mHandler.sendMessage(mHandler.obtainMessage());
						}
					});
					d.start();
				} else {
					curPage = curPage - 1;
				}
			}
		}

	};

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			addItems(commentList);
			loadingLayout.setVisibility(View.GONE);
		}
	};

	/**
	 * @param items
	 */
	public void addItems(List<Comment> items) {
		for (Comment comment : items) {
			this.add(comment);
		}
		this.notifyDataSetChanged();
	}

}
